﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;

namespace ArticleWeb
{
    public static class QueryExtension
    {
        public static IQueryable<T> Equal<T, TValue>(this IQueryable<T> query, Expression<Func<T, TValue>> selector, object value)
        {
            if (value == null)
                return query;
            if (value.GetType() != typeof(TValue))
            {
                value = Convert.ChangeType(value, typeof(TValue));
            }
            var expp = Expression.Constant(value, typeof(TValue));
            var exp3 = Expression.Equal(selector.Body, expp);
            var expEnd = (Expression<Func<T, bool>>)Expression.Lambda(exp3, selector.Parameters[0]);
            return query.Where(expEnd);
        }

        public static IQueryable<T> NotEqual<T, TValue>(this IQueryable<T> query, Expression<Func<T, TValue>> selector, object value)
        {
            if (value == null)
                return query;
            if (value.GetType() != typeof(TValue))
            {
                value = Convert.ChangeType(value, typeof(TValue));
            }
            var expp = Expression.Constant(value, typeof(TValue));
            var exp3 = Expression.NotEqual(selector.Body, expp);
            var expEnd = (Expression<Func<T, bool>>)Expression.Lambda(exp3, selector.Parameters[0]);
            return query.Where(expEnd);
        }

        public static IQueryable<T> LessThanOrEqual<T, TValue>(this IQueryable<T> query, Expression<Func<T, TValue>> selector, object value)
        {
            if (value == null)
                return query;
            if (value.GetType() != typeof(TValue))
            {
                value = Convert.ChangeType(value, typeof(TValue));
            }
            var expp = Expression.Constant(value, typeof(TValue));
            var exp3 = Expression.LessThanOrEqual(selector.Body, expp);
            var expEnd = (Expression<Func<T, bool>>)Expression.Lambda(exp3, selector.Parameters[0]);
            return query.Where(expEnd);
        }
        public static IQueryable<T> GreaterThanOrEqual<T, TValue>(this IQueryable<T> query, Expression<Func<T, TValue>> selector, object value)
        {
            if (value == null)
                return query;
            if (value.GetType() != typeof(TValue))
            {
                value = Convert.ChangeType(value, typeof(TValue));
            }
            var expp = Expression.Constant(value, typeof(TValue));
            var exp3 = Expression.GreaterThanOrEqual(selector.Body, expp);
            var expEnd = (Expression<Func<T, bool>>)Expression.Lambda(exp3, selector.Parameters[0]);
            return query.Where(expEnd);
        }

        public static IQueryable<T> Between<T, TValue>(this IQueryable<T> query, Expression<Func<T, TValue>> selector, object valueFrom, object valueTo)
        {
            return query.GreaterThanOrEqual(selector, valueFrom).LessThanOrEqual(selector, valueTo);
        }
    }
}